<!-- 自定义标准图表 设置百分百宽度 通过平台组件来适配尺寸 可以替换数据以及设置高级属性自定义扩展 详细查看组件示例.txt
  注:如需设置图片存放文件夹名称需应与当前文件名一致(1并在aircim.webpack.build.component.config.js配置需要打包的文件 2.npm run lib_c 执行打包命令  最后的组件会打包在components/grids中)-->
<template>
  <div class="scale-box" :id="dynamicIdName()">
    <div
        class="service-efficiency-box animate__animated"
        :class="dynamicClassName"
        style="width: 460px;height: 450px;"
        :id="dynamicChildName()"
    >
      <h1 class="h1_title">
        <span>即办能力</span>
      </h1>
      <div class="show-panel">
        <div class="show-panel-item">
          <div class="show-panel-item-title">即办率</div>
          <div class="show-panel-item-content">
            <em>84.20</em>%
          </div>
        </div>
        <div class="show-panel-item">
          <div class="show-panel-item-title">即办率同比</div>
          <div class="show-panel-item-content up">
            <em>2.28</em>%
          </div>
        </div>
      </div>
      <h2 class="h2_title"><span>各部门即办率排名</span></h2>
      <div class="box-container" style="width: 100%">
        <BarHorizontal :list="barList"></BarHorizontal>
      </div>
    </div>
  </div>
</template>
<script>
//引入封装方法
import encapsulation from "../../utils/aircimGird";
import BarHorizontal from "./BarHorizontal.vue";
export default {
  //混入封装方法
  mixins: [encapsulation],
  components: {
    BarHorizontal,
  },
  data: function() {
    return {
      resizeObserver: null,
      //动态Id
      timestamp: null,
      barList:[
        {
          name:'部门1',
          value:'90.12',
          unit:'%'
        },
        {
          name:'部门2',
          value:'50',
          unit:'%'
        },
        {
          name:'部门3',
          value:'20',
          unit:'%'
        },
      ]
    };
  },
  mounted() {
    this.timestamp = new Date().getTime();
    this.$nextTick(() => {
      // 监听盒子变化
      this.listenChangeFn();
    });
  },
  methods: {
    //监听外部盒子的大小 然后对数据井行缩放
    scaleFn() {
      const parentBox = document.getElementById(`dynamic-${this.timestamp}`);
      const childBox = document.getElementById(
          `dynamicChild-${this.timestamp}`
      );
      const parentWidth = parentBox.offsetWidth;
      const parentHeight = parentBox.offsetHeight;
      const childWidth = childBox.offsetWidth;
      const childHeight = childBox.offsetHeight;
      let scaleX, scaleY;
      if (parentWidth < childWidth || parentHeight < childHeight) {
        const scaleBasedOnWidth = parentWidth / childWidth;
        const scaleBasedOnHeight = parentHeight / childHeight;
        scaleX = scaleBasedOnWidth;
        scaleY = scaleBasedOnHeight;
      } else {
        scaleX = parentWidth / childWidth;
        scaleY = parentHeight / childHeight;
      }
      childBox.style.transform = `scale(${scaleX}, ${scaleY})`;
    },
    // 监听盒子变化
    listenChangeFn() {
      let that = this;
      const element = document.getElementById(`dynamic-${this.timestamp}`);
      // console.log(element, "element");
      if (element) {
        new ResizeObserver(() => {
          // 处理元素大小变化的逻辑
          that.scaleFn();
        }).observe(element);
      }
    },
    // 绑定动画样式 animate__flash
    bindAnimationFn(animateName) {
      if (animateName != null) {
        this.dynamicClassName = animateName;
      }
    },
    //定义唯一ID名
    dynamicIdName() {
      return `dynamic-${this.timestamp}`;
    },
    //定义唯一ID名
    dynamicChildName() {
      return `dynamicChild-${this.timestamp}`;
    },
  },
  // 销毁监听
  beforeDestroy() {
    // 组件销毁前停止监听
    if (this.resizeObserver) {
      this.resizeObserver.disconnect();
    }
    if (this.requestTime != null) {
      clearInterval(this.requestTime);
    }
  },
};
</script>
<style lang="scss" scoped>
@import "../../static/assets/common.scss";
</style>
<style lang="scss" scoped>
.scale-box {
  width: 100%;
  height: 100%;
}

.service-efficiency-box {
  position: absolute;
  padding: 0 20px;
  box-sizing: border-box;
  overflow: hidden;
  transform-origin: left top;
  background-image: url(../../../thumbnail/backImg/new-border-mini-green.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  color: #fff;
}
.box-container{
  width: 100%;
  height: 100%;
}
.show-panel{
  margin: 12px 0 24px 0;
  background-image: url("../../static/assets/ServiceWorkAbility/work_bg.png");
}
</style>
